<template>
  <div class="app-container customer-div">
    <div>
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="50px">
        <el-form-item label="日期：">
          <el-date-picker
            v-model="listQuery.date"
            type="monthrange"
            format="yyyy 年 MM 月"
            value-format="yyyy-MM"
            range-separator="至"
            start-placeholder="开始月份"
            end-placeholder="结束月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
          <el-button
            @click="handleSearchList()"
            type="primary">
            导出
          </el-button>
          <el-button
            @click="showPop('add','')"
            type="primary">
            新增月目标
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="月份"  align="center" min-width="100">
          <template slot-scope="scope">
              {{scope.row.time}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="产品线" min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsName}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="目标销量" min-width="100">
          <template slot-scope="scope">
            {{scope.row.targetNum}}
          </template>
        </el-table-column>
        <el-table-column label="实际完成总量" align="center" min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column min-width="100" label="总完成率" align="center">
          <template slot-scope="scope">
            {{scope.row.completionRate}} %
          </template>
        </el-table-column>
        <el-table-column min-width="100" label="负责人" align="center">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column min-width="100" label="创建时间" align="center">
          <template slot-scope="scope">
            {{scope.row.createTime}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="100"
          label="操作">
          <template  slot-scope="scope">
              <el-button type="text" @click="showPop('edit',scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20,50,100]"
        :total="count">
      </el-pagination>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      title="月目标设置"
      :visible.sync="dialogVisible"
      width="400px">
      <el-form :model="form" size="mini" label-width="100px" :rules="rules" ref="form">
        <el-form-item label="月份：" prop="time">
          <el-date-picker
            value-format="yyyy-MM-dd"
            :disabled="popType=='edit'"
            v-model="form.time"
            type="month"
            placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品线：" prop="goodsLineId">
            <el-select v-model="form.goodsLineId" :disabled="popType=='edit'">
              <el-option v-for="item in goodsLineList" :label="item.cddValue" :value="item.cddId" :key="item.cddId"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="月目标销量：" prop="targetNum">
            <el-input v-model="form.targetNum" style="width:150px;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

import {getCustomerSetting,getQiniuToken,agentProcessBillList,agentProcessAddApplyPay} from '@/api/partner'
import {mouthTargetList,addMouthTarget} from '@/api/company'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'

const defaultListQuery={
  "date" : [], //单据日期
  "pageNum" : 1,
  "pageSize" : 10
};

const defaultForm={
  targetId:0,
  time:'',
  goodsLineId:'',
  targetNum:'',
};

export default {
  name: "index",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],
      btnLoading:false,
      dialogVisible:false,
      form:JSON.parse(JSON.stringify(defaultForm)),
      rules:{
        time:[{required: true, trigger: 'change' ,message: '此项必填',}],
        goodsLineId:[{required: true, trigger: 'change' ,message: '此项必填',}],
        targetNum:[{required: true, trigger: 'blur' ,message: '此项必填',}],
      },
      goodsLineList:[],
      popType:''
    }
  },

  computed: {

  },
  created() {
    this.getCustomerSettingList()
    this.getList();
  },
  methods:{
    //获取客户配置
    getCustomerSettingList() {
      getCustomerSetting().then((res) => {
        res.forEach((el) => {
          if (el.cdtCode == 'goods_line') {
            this.goodsLineList = el.data
          }
        });
      })
    },

    showPop(type,item){
      this.dialogVisible = true;
      this.popType = type;
      if(type == 'add'){
        this.form = JSON.parse(JSON.stringify(defaultForm))
      }else{
        this.form = JSON.parse(JSON.stringify(item))
      }
    },
    handleClose(){
      this.dialogVisible = false;
    },
    handleConfirm(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          addMouthTarget(this.form).then(()=>{
            this.btnLoading = false;
            this.dialogVisible = false;
            this.getList()
          }).catch(()=>{
            this.btnLoading = false;
          })

        }
      })
    },
    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
         mouthTargetList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
    },
  },
}
</script>

<style scoped lang="scss">

</style>
